package com.hnkywl.sts.dao.cwgl.kmys;

import com.hnkywl.sts.entity.cwgl.kmys.Ljkmdmys;
import com.hnkywl.sts.entity.cwgl.kmys.Nbgskmdmys;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.hibernate.SQLQuery;
import org.hibernate.tool.hbm2x.StringUtils;
import org.hibernate.transform.AliasToEntityMapResultTransformer;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: ccr
 * Date: 13-5-14
 * Time: 下午3:43
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class NbgskmdmysDao extends SimpleHibernateTemplate<Nbgskmdmys,Long>{

    public Page<Map> findAll(Long syId, Long xyId,String khkmdm,String gyskmdm ,int pageNo,int pageSize) {
        Page<Map> page = new Page<Map>(pageNo,pageSize,0);
        String from = "from t_Sys_Nbgskmdmys nbgskmdmys" +
                "  left join t_sys_gsjg sy on (nbgskmdmys.syid = sy.id and sy.type = 'nbgs')" +
                "  left join t_sys_gsjg xy on (nbgskmdmys.xyid = xy.id and xy.type = 'nbgs')";
        StringBuilder where = new StringBuilder();
        if (syId != null) {
            if(where.length() != 0)
                where.append(" and ");
            where.append("nbgskmdmys.syId = :syId");
        }
        if(xyId != null) {
            if(where.length() != 0)
                where.append(" and ");
            where.append("nbgskmdmys.xyId = :xyId");
        }
        if(StringUtils.isNotEmpty(khkmdm)){
            if(where.length() != 0)
                where.append(" and ");
            where.append("nbgskmdmys.khkmdm like '%" + khkmdm + "%'");
        }
        if(StringUtils.isNotEmpty(gyskmdm)){
            if(where.length() != 0)
                where.append(" and ");
            where.append("nbgskmdmys.gyskmdm like '%" + gyskmdm + "%'");
        }
        String orderBy = " order by sy.pxh,xy.pxh asc";
        String countSql = "select count(1) " + from + (where.length()!=0?" where ":"") + where;
        String sql = "select nbgskmdmys.id   as \"nbgskmdmys.id\",nbgskmdmys.syid as \"nbgskmdmys.syId\",nbgskmdmys.xyid as \"nbgskmdmys.xyId\"," +
                "nbgskmdmys.khkmdm as \"nbgskmdmys.khkmdm\",nbgskmdmys.gyskmdm as \"nbgskmdmys.gyskmdm\",sy.jc as \"syJc\",xy.jc as \"xyJc\""+ from + (where.length()!=0?" where ":"") + where + orderBy;
        SQLQuery countSqlQuery = getSession().createSQLQuery(countSql);
        SQLQuery sqlQuery = getSession().createSQLQuery(sql);
        if (syId != null) {
            countSqlQuery.setParameter("syId",syId);
            sqlQuery.setParameter("syId",syId);
        }
        if(xyId != null) {
            countSqlQuery.setParameter("xyId",xyId);
            sqlQuery.setParameter("xyId",xyId);
        }
        if(page.isFirstSetted())
            sqlQuery.setFirstResult(page.getFirst());
        if(page.isPageSizeSetted())
            sqlQuery.setMaxResults(page.getPageSize());
        sqlQuery.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
        page.setTotalCount(((BigDecimal)countSqlQuery.uniqueResult()).intValue());
        page.setResult(sqlQuery.list());
        return page;
    }

    public boolean checkUnique(Nbgskmdmys nbgskmdmys){
        if(nbgskmdmys.getId() == null){
            String hql = "from Nbgskmdmys where syId = ? and xyId = ?";
            return existsQuery(hql,nbgskmdmys.getSyId(),nbgskmdmys.getXyId());
        } else {
            String hql = "from Nbgskmdmys where syId = ? and xyId = ? and id <> ?";
            return existsQuery(hql,nbgskmdmys.getSyId(),nbgskmdmys.getXyId(),nbgskmdmys.getId());
        }
    }

    public Nbgskmdmys findKmdmByGsjgId(Long nbgsId, Long qyjgId, Integer lx) {
        String hql = "from Nbgskmdmys where syId = ? and xyId = ?  ";
        return find(hql, nbgsId, qyjgId).size() > 0 ? (Nbgskmdmys) find(hql, nbgsId, qyjgId).get(0) : new Nbgskmdmys();
    }
}
